﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using Infrastructure;

namespace OpenAuth.WebApi.Areas.HisApi.Controllers
{
    [Route("HisApi/[controller]/[action]")]
    [ApiController]
    
    public class TriageController : ControllerBase
    {
        //MZHSZ操作类 led大屏
        private YIN_MZJHJLKApp MApp;
        //医生站库中挂号列表
        private OUTP_JZJLKApp outApp;
        //本地签到表
        private TriageSignApp TApp;
        //排班记录表
        private OUTP_FZ_YSDLXXApp YApp;
        //获取充值记录app
        private CMZXX007App cmzxx007App;

        /// <summary>
        /// 自助机查询当前患者是否今天有就诊中得记录
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="name">患者姓名</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryPatiendJZ(string patid, string name)
        {
            //获取医生站中是否存在需要二次就诊得记录
            var result = outApp.QuerySingle(patid, name);
           
            //存在初次就诊得记录
            if (result!=null)
            {
                //获取排队人数
                var peoCount = TApp.QueryList(result.YSDM, DateTime.Now.ToString("yyyyMMdd"));
                return new { Code = 200, RequestId = Guid.NewGuid().ToString(), Result = result,Count= peoCount.Count(), message ="获取数据成功" }.ToJson();
            }
            else
            {
                return new { Code = 400, RequestId = Guid.NewGuid().ToString(), Message = "没有查询到数据" }.ToJson();
            }
        }
        /// <summary>
        /// 自助机就诊签到方法
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="GHXH">挂号序号</param>
        /// <returns></returns>
        [HttpGet]
        public string PutPatiendSign(string patid, string GHXH)
        {
            //获取本地表中是否已经签到过
            var result = TApp.QuerySingle(patid, GHXH);
            
            if (result==null)
            {
                var temp = outApp.QuerySingleByGHXH(patid, GHXH);
                TApp.AddSign(temp);
                return new { Code = 200, RequestId = Guid.NewGuid().ToString(),message="签到成功"}.ToJson();
            }
            else
            {
                return new { Code = 400, RequestId = Guid.NewGuid().ToString(),Message="已经签到" }.ToJson();
            }
        }
        /// <summary>
        /// 医生端叫号方法
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="GHXH">挂号序号</param>
        /// <returns></returns>
        [HttpGet]
        public string PutPatiendCall(string patid, string GHXH)
        {                     
            //把刚才更新得记录获取出来
            var result = TApp.QuerySingle(patid, GHXH);
            //大屏幕表
            var temp = MApp.QuerySingle(patid,GHXH);
            if (result!=null)
            {
                //不存在记录并且是没有叫过号得
                if (temp==null)
                {
                    MApp.AddSign(result);
                    //更新本地库中得签到记录
                    TApp.Update(patid, GHXH);
                    return new { Code = 200, RequestId = Guid.NewGuid().ToString(),message="首次叫号成功" }.ToJson();
                }
                else
                {
                    MApp.Update(patid, GHXH, temp.JHCS);
                    //更新本地库中得签到记录
                    TApp.Update(patid, GHXH);
                    return new { Code = 200, RequestId = Guid.NewGuid().ToString(),message="再次叫号成功" }.ToJson();
                }
                
            }
            else
            {
                return new { Code = 400, RequestId = Guid.NewGuid().ToString(),message="没有签到记录" }.ToJson();
            }           
        }
        /// <summary>
        /// 医生端获取已经签到得患者
        /// </summary>
        /// <param name="ysdm">医生代码</param>
        /// <param name="date">日期：20191206</param>
        /// <returns></returns>
        [HttpGet]
        public string QuerySignPatiend(string ysdm,string date)
        {
            var result = TApp.QueryList(ysdm, date);
            if (result.Count()>0)
            {
                return new { Code = 200, RequestId = Guid.NewGuid().ToString(),Result=result, message = "查询成功" }.ToJson();

            }
            else
            {
                return new { Code = 400, RequestId = Guid.NewGuid().ToString(), message = "查询失败" }.ToJson();

            }

        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="ysdm"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryDocLogin(string ysdm, string pwd)
        {
            var model = YApp.QuerySingle(ysdm);
            if (model!=null)
            {
                return new { Code = 200, RequestId = Guid.NewGuid().ToString(), Result = model, message = "登录成功" }.ToJson();

            }
            else
            {
                return new { Code = 400, RequestId = Guid.NewGuid().ToString(), message = "登录失败" }.ToJson();

            }
        }
        //public string QueryChargeList(string patid, string name)
        //{
        //    string ksrq = DateTime.Now.AddMonths(-1).ToString("yyyyMMdd");
        //    string jsrq = DateTime.Now.ToString("yyyyMMdd");
        //}
        

        /// <summary>
        /// 构造注入方法
        /// </summary>
        /// <param name="_app"></param>
        /// <param name="_outApp"></param>
        public TriageController(YIN_MZJHJLKApp _app,OUTP_JZJLKApp _outApp,TriageSignApp _tApp,OUTP_FZ_YSDLXXApp _YApp,CMZXX007App _cmzxx007App)
        {
            MApp = _app;
            outApp = _outApp;
            TApp = _tApp;
            YApp = _YApp;
            cmzxx007App = _cmzxx007App;
        }
    }
}